<template>
	<view class="pB50 problemarr">
		<view>
			<form @submit="formSubmit" @reset="formReset">
				<view class="top-title">安全培训答题（一共{{ sumn }}道）</view>
				<view class="uni-form-item uni-column p10" v-for="(item,index) in subjlist">
					<view class="title">{{ index+1 }}/{{ sumn }}：{{item.question}}</view>
					<radio-group name="radio" @change="choiceanswer">
						<label v-for="(radio,radioindex) in item.options">
							<view>
								<radio  :checked="radioindex == item.answer" :value="index+'-'+radio.value"  />{{radio.tik==0 ? 'A':''}} {{radio.tik==1 ? 'B':''}}{{radio.tik==2 ? 'C':''}}{{radio.tik==3 ? 'D':''}}:{{radio.name}}
								</br>
							</view>
							
						</label>
					</radio-group>
				</view>

				<view class="uni-btn-v" v-if="subjlist.length>0">
					<button form-type="submit" class="submit w90" style="margin: 0 auto;">提交</button>
				</view>
			</form>
		</view>
	</view>
</template>
<script>
	import {questiontrain,questionlist} from '@/common/api/topics.js'
export default {
	data() {
		return {
			sumn: 0,
			nown: 1,
			subjlist:[],
			options:[],
			tmpoptions:"",
			score:0,
			wrong:[],
			submitchanger:true,
			userinfo:[],
			page:1,
			pageSize:10,
			results:0,
			companyid:0,
			appointmentid:'',
			showtype:"",
			meeting_code:'',
			showclick:true,
		};
	},
	onShow() { 
		this.score=0;
	},
	onLoad(event){
		uni.getStorage({
			key: 'userinfo',
			success: res => {
				this.userinfo = res.data
			}
		});
		// this.companyid = this.userinfo.companyid
		if(event.details){
			let urldata = JSON.parse(event.details)
			if(urldata['type'] == 'yq'){
				this.companyid = urldata['companyid']
			}
			this.companyid = urldata['companyid'];
			this.showtype = urldata['type'];
			console.log(this.showtype)
			if(urldata['appointmentid']){
				this.appointmentid=urldata['appointmentid'];
			}
			if(urldata['meeting_code']){
				this.meeting_code=urldata['meeting_code'];
			}
			
			// console.log(JSON.parse(event.details))			
			// companyid: 1
			// type: "yq"
		}
		questionlist({
			'companyid':this.companyid,
			'page':this.page,
			'pagesize':this.pageSize,
			'type':'train'
		}).then(res=>{
			if(res.data.code==200){
				this.score=0;
				this.subjlist = res.data.data
				this.sumn = this.subjlist.length
				this.tmpoptions = this.subjlist.options
				console.log(this.subjlist)
				for (let i = 0; i < this.subjlist.length; i++) {
					if(this.subjlist[i].options == ''){
						continue;
					}else{
						this.subjlist[i].ucehcked = true;
						this.subjlist[i].ischeck = 1;
						this.subjlist[i].options = JSON.parse(this.subjlist[i].options);
					}
				}
				// console.log(this.subjlist)
				
			}
		})
	},
	methods: {
		/*提交*/
		formSubmit(e) {
			var topicsscore = 100/this.subjlist.length
			var countscore = 0 
			let wronglist = []
			// for (let i = 0; i < this.wrong.length; i++) {
			// 	wronglist.push(this.subjlist[this.wrong[i]])
			// }
			// console.log(countscore)
			var nocheckcount = 0 
			if(this.showclick){
				this.showclick=false;
			/*tips no checked*/ /*计算分数*/
			for (let i = 0; i < this.subjlist.length; i++) {
				if(typeof(this.subjlist[i].ischeck) == 'undefined' && this.subjlist[i].ischeck != 1){
					 nocheckcount++
				}
				if(this.subjlist[i].ucehcked == true){
					console.log(this.subjlist[i])
					this.score++
				
				}else{
					wronglist.push(this.subjlist[i])
				}
			}
			if(nocheckcount != 0){
				this.showclick=true;
				uni.showToast({
				    icon: 'none',
				    title: '还有'+nocheckcount+'道题未做',
				    duration: 2000
				});
				this.score = 0
				countscore = 0 
				return
			}
			
			countscore = this.score*topicsscore
			/*保留两位小数*/
			countscore = countscore.toFixed(0);
			questiontrain({
				companyid:this.companyid,
				appointmentid:this.appointmentid,
				answer:countscore,
				type:this.showtype,
			}).then(res=>{ 
				this.showclick=true;
				if(parseInt(res.data.code) == 200 ){
					this.results = 0 
					uni.reLaunch({
						url: '/pages/subject/answerResults?score='+countscore+'&companyid='+this.companyid+'&appointmentid='+this.appointmentid+'&wronglist='+JSON.stringify(wronglist)+'&results='+this.results+'&type='+this.showtype+'&meeting_code='+this.meeting_code
					});
				}else{
					this.results = 1 /*go*/
					uni.reLaunch({
						url: '/pages/subject/answerResults?score='+countscore+'&companyid='+this.companyid+'&appointmentid='+this.appointmentid+'&wronglist='+JSON.stringify(wronglist)+'&results='+this.results+'&type='+this.showtype+'&meeting_code='+this.meeting_code
					});
				}
			}) 

		}

		},
		formReset(e) {
			console.log('清空数据');
		},
		/*选答案 判断*/
		choiceanswer(event){
			var res  = event.detail.value
			var tmparr = res.split('-')
			/*ischeck 1-ischeck 题目*/
			this.subjlist[tmparr[0]]['ischeck'] = 1
			/*[index-options,radio-nowcheckvalue]*/
			/*获取被选中value  然后检查答案是否正确  正确++ 错误--*/
			/*获取打错题目 -  *判断是否正确  - push到错误题  答对-None -再次答对-*/
			var trueanswer =  this.subjlist[tmparr[0]].answer
			if(tmparr[1] == trueanswer){
				this.subjlist[tmparr[0]]['ucehcked'] = true
			}else{
				this.subjlist[tmparr[0]]['ucehcked'] = false
			}
			
			console.log(this.subjlist[tmparr[0]].ucehcked)
			
			
			// if(tmparr[1] == trueanswer){
			// 	let  wrongindex = this.wrong.indexOf(tmparr[0])
			// 	if(wrongindex!= -1){
			// 		this.wrong.splice(wrongindex,1)
			// 	}
			// 	this.score++
			// 	return
			// }else{
			// 	this.wrong.push(tmparr[0])
			// 	this.score--
			// }
		}
	}
};
</script>

<style>
.top-title {
	margin: 10px 0 0 10px;
}
.uni-form-item .title {
	padding: 0;
}
uni-radio-group,
uni-checkbox-group {
	width: 100%;
	/* justify-content: space-between; */
	/* margin-left: 10%; */
	display: flex;
	justify-content: space-around;
}
.submit {
	position: relative;
	display: block;
	/* margin-left: 10px;
	margin-right: 10px; */
	padding-left: 14px;
	padding-right: 14px;
	box-sizing: border-box;
	font-size: 18px;
	text-align: center;
	text-decoration: none;
	line-height: 2.55555556;
	border-radius: 5px;
	-webkit-tap-highlight-color: transparent;
	overflow: hidden;
	color: #000;
	background-color: #f8f8f8;
	cursor: pointer;
	bottom: 10px;
	color: rgba(255, 255, 255, 1);
	background-color: rgba(42, 130, 228, 1);
	border-radius: 5px;
	font-size: 15px;
}
.uni-btn-v{
	position: fixed;
	width: 100%;
	bottom: 0;
	background: #fff;
}
.problemarr .uni-form-item uni-radio-group{display: block;}
.problemarr .uni-form-item uni-radio-group .uni-label-pointer{padding-left: 5px;}
.problemarr .uni-form-item uni-radio .uni-radio-input{width: 20px;height: 20px;}
</style>
